<?php
namespace app\admin\addons;
class FieldHand
{
    /**创建插件
     * @param array $array
     * @return array
     */
    public static function CreateAddons(array $array){
        //检查是否有地址
        if(empty($array['name'])){
            return [false,'没有设置标识'];
        }
        //检查是否有文件夹
        $addonsPath = root_path().'addons'.DIRECTORY_SEPARATOR;
        if(!is_dir($addonsPath)){
            @mkdir($addonsPath,0777,true);
        }else{
            @chmod($addonsPath,0777);
        }
        list($bool,$hasarray) = self::getDir($addonsPath);

        if(in_array($array['name'],$hasarray)){
            return [false,'已有本插件'];
        }
        //创建插件目录
        $addonsPath = $addonsPath.$array['name'];
        if(!is_dir($addonsPath)){
            @mkdir($addonsPath,0777,true);
        }else{
            @chmod($addonsPath,0777);
        }
        $namespace = $array['name'];
        //获取
        $batchreplace = [
            "{{NAMESPACE}}"=>$namespace,
            "{{NAME}}"=>$array['name'],
            "{{TITLE}}"=>$array['title'],
            "{{INTRO}}"=>$array['intro'],
            "{{AUTHOR}}"=>$array['author'],
            "{{VERSION}}"=>$array['version']??'1.0.0',
            "{{STATE}}"=>0,
            "{{URL}}"=>'',
            "{{RELYON}}"=>empty($array['relyon'])?'':implode(',',$array['relyon'])
        ];
        $pluginstr = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'sub'.DIRECTORY_SEPARATOR.'Plugin.txt');
        $pluginstr = self::BatchReplace($pluginstr,$batchreplace);
        $pluginPath = $addonsPath.DIRECTORY_SEPARATOR.'Plugin.php';
        $inistr = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'sub'.DIRECTORY_SEPARATOR.'ini.txt');
        $inistr = self::BatchReplace($inistr,$batchreplace);
        $iniPath = $addonsPath.DIRECTORY_SEPARATOR.'info.ini';
        $configstr = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'sub'.DIRECTORY_SEPARATOR.'config.txt');
        $configstr = self::BatchReplace($configstr,$batchreplace);
        $configPath = $addonsPath.DIRECTORY_SEPARATOR.'config.php';
        $bool1 = file_put_contents($pluginPath,$pluginstr);
        $bool2 =file_put_contents($iniPath,$inistr);
        $bool3 =file_put_contents($configPath,$configstr);
        if($bool1&&$bool2&&$bool3){
            return [true,'创建成功'];
        }else{
            return [false,'创建失败'];
        }
    }

    /**批量替换
     * @param string $str
     * @param array $array
     * @return array|string|string[]
     */
    protected static function BatchReplace(string $str,$array=[]){
        foreach ($array as $key=>$value){
            $str = str_replace($key,$value,$str);
        }
        return $str;
    }

    /**获取文件
     * @param string $dirPath
     * @return array
     */
    public static function getDir(string $dirPath):array{
        if(!is_dir($dirPath)){
            return [false,'地址不存在'];
        }
        $array = scandir($dirPath);
        $newarray = [];
        foreach ($array as $value){
            if($value!=='.'&&$value!=='..'){
                $newarray[] = $value;
            }
        }
        return [true,$newarray];
    }

    /**移动目录
     * @param $fromPath
     * @param $toPath
     * @param bool $deleteFile
     * @return bool
     */
    public static function moveDir($fromPath,$toPath,$deleteFile=true){
        if(!(is_dir($fromPath)||is_file($fromPath))){
            return true;
        }
        if(!is_dir($toPath)){
            @mkdir($toPath,0777,true);
        }else{
            @chmod($toPath,0777);
        }
        $files = scandir($fromPath);
        foreach ($files as $value){
            if($value!=='.'&&$value!=='..'){
                if(is_dir($fromPath.DIRECTORY_SEPARATOR.$value)){
                    self::moveDir($fromPath.DIRECTORY_SEPARATOR.$value,$toPath.DIRECTORY_SEPARATOR.$value,$deleteFile);
                    if($deleteFile){
                        @rmdir($fromPath.DIRECTORY_SEPARATOR.$value);
                    }
                }else{
                    self::moveFile($fromPath.DIRECTORY_SEPARATOR.$value,$toPath.DIRECTORY_SEPARATOR.$value,$deleteFile);
                }
            }
        }
        return true;
    }

    /**移动文件
     * @param $fromFile
     * @param $toFile
     * @param bool $deleteFile
     * @return bool
     */
    public static function moveFile($fromFile,$toFile,$deleteFile=true){
        if(is_file($fromFile)){
            copy($fromFile,$toFile);
            @chmod($fromFile,0777);
            if($deleteFile){
                @unlink($fromFile);
            }
        }
        return true;
    }
    public static function setIniConfig(string $fieldPath,array $array = []){
        @file_put_contents($fieldPath,self::arrayToIniStr($array));
        return true;
    }
    protected static function arrayToIniStr($array=[]){
        $str = '';
        foreach ($array as $key=>$value){
            if(is_array($value)){
                $str .= "[{$key}]".PHP_EOL;
                $str .= self::arrayToIniStr($value).PHP_EOL;
            }else{
                $str .= "{$key} = {$value}".PHP_EOL;
            }
        }
        return $str;
    }
}